// pages/player/player.js
let musiclist = [];
const Bgm=wx.getBackgroundAudioManager();
//正在播放的
let playIndex = 0;
const app =getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    picUrl: '',
    isplaying:false,
    islyshow: false,//geci
     lys:""   
  }, 
  timeUpdate(e){
    this.selectComponent('.liandong').update(
      e.detail.currtime
    )
  },
  //歌词蓝洞
  
  //歌词显示
  onchangely(){
    this.setData({
      islyshow:!this.data.islyshow
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    playIndex = options.index
    musiclist = wx.getStorageSync('musiclist')
    console.log(options.musicid)
    this._loadMusicDetail(options.musicid)
  },
  _loadMusicDetail(musicId) {
    Bgm.stop()
    let music = musiclist[playIndex]
    console.log(music);
    wx.setNavigationBarTitle({
      title: music.name
    })
    this.setData({
      picUrl: music.al.picUrl,
      isplaying:false
    })
    app.setplayid(musicId)
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.callFunction({
      name: 'music',
      data: {
        musicId,
        $url: 'musicUrl'
      }
    }).then((res)=>{
      console.log(res);
      let rt=res.result
      if(rt.data[0].url!==null){
        Bgm.src = rt.data[0].url
        Bgm.title = music.name
        Bgm.coverImgUrl = music.al.picUrl
        Bgm.singer = music.ar[0].name
        Bgm.epname = music.al.name
        this.setData({
          isplaying: true
        })
        wx.hideLoading()
        //加载歌词
        wx.cloud.callFunction({
          name:'music',
          data:{
            $url:'ly',
            musicId:musicId
          }
        }).then((res)=>{
          console.log(res)
          let lys="暂无歌词"
          const lrc = JSON.parse(res.result).lrc
          if(lrc){
            lys=lrc.lyric
          }
          this.setData({
            lys
          })
        })
      }else{
        wx.hideLoading()
        wx.showModal({
          title: '啊哦',
          content: '这个没有资源呢',
          success(res) {
            if (res.confirm) {
              wx.navigateBack({
                delta: 1
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        
      }
    })
  },
  toggleplay(){
    if(this.data.isplaying){
      Bgm.pause()
    }else{
      Bgm.play()
    }
    this.setData({
      isplaying:!this.data.isplaying
    })
  },
  onPrev(){
    playIndex--
    if(playIndex<0){
      playIndex=musiclist.length-1
s    }
    this._loadMusicDetail(musiclist[playIndex].id)
  },
  onNext(){
    playIndex++
    if (playIndex === musiclist.length){
      playIndex = 0
    }
    this._loadMusicDetail(musiclist[playIndex].id)
  },
  inplay(){
    this.setData({
      isplaying:true
    })
  },
  
  inpause(){
    this.setData({
      isplaying: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})